package com.itcam.lease.web.admin.schedule;

import com.itcam.lease.model.entity.LeaseAgreement;
import com.itcam.lease.model.enums.LeaseStatus;
import com.itcam.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduleTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * 定时检查租约状态
     * 每天0点执行
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        // 创建一个LambdaUpdateWrapper对象，用于构造更新条件
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        // 获取当前时间
        Date now = new Date();
        // 设置更新条件，租约结束日期小于等于当前时间
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        // 设置更新条件，租约状态为已签约
        updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        // 设置更新条件，租约状态为已签约或解约中
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

        // 调用leaseAgreementService的update方法，执行更新操作
        leaseAgreementService.update(updateWrapper);
    }
}

